def collect():
    userName = input("请输入用户名：")
    password = input("请输入密码：")
    return userName, password


while True:
    choose = input("请选择1.注册 2.登录 0.退出：")
    # 注册
    if choose == '1':
        print("##########注册##########")
        userName, password = collect()
        with open("user.txt", "a", encoding="utf-8") as f:
            info = [userName, '|', password, '\n']
            f.writelines(info)
        print("注册成功！")
    # 登录
    elif choose == '2':
        print("##########登录##########")
        is_load = False
        userName, password = collect()
        with open("user.txt", "r", encoding="utf-8") as f:
            while True:
                buf = f.readline()
                # 读到一行信息
                if buf:
                    buf_split = buf.strip("\n").split('|')
                    # 匹配一下用户和密码
                    if buf_split[0] == userName and buf_split[1] == password:
                        print("登录成功！")
                        is_load = True
                        break
                # 此时读到结尾了，且还没跳出读取文件的循环
                else:
                    print("用户名或密码有误！")
                    break
        if is_load:
            break
    # 退出
    elif choose == '0':
        print("期待您的下次使用!")
        break
    else:
        print("您输入的数据不对噢，请重新输入！")
    print('-' * 21)
